<?php
	/**
 	 * Updater controller class
 	 */
	class SystemUpdaterController extends Controller
	{
		/**
		 * system logging usage
		 */
		public $use_logging = false;
		
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_SYSTEM_UPDATER;
		
		/**
	 	 * controller name
	 	 */
		public $name = 'main';
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * current module
	 	 */
		public $module = 'administration';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ('html', 'htmlx', 'grids', 'blanks');
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ('');
		
		/**
		 * Defines whether to check access (default: true)
		 *
		 * @var boolean
		 */
		public $checkAccess = false;

		/**
		 * defines whether the controller is to be cached
		 *
		 * @var boolean
		 */
		public $isCached = false;
		
		/**
	 	 * index page
	 	 */
		function Index()
		{
			$remote_dir = 'D:/Internet/home/Starnet/www';
			
			//paths with the files to update
			$updatePaths = array
			(
				LANG_GUIDE => '/app/views/guide',
				LANG_HELPERS => '/app/includes/helpers',
//				LANG_COMPONENTS => '/app/includes/components',
			);
			
			//if is post back
			if ($this->request->post)
			{
				foreach($this->request->post[files] as $file=>$value)
				{
					//backups if the old file is replaced by the new one
					if (file_exists('.'.SEPARATOR.$file))
					{
						copy('.'.SEPARATOR.$file, '.'.SEPARATOR.$file.'.bak');
					}
					
					//updates the file
					copy($remote_dir.$file, '.'.SEPARATOR.$file);
					
					//sets the modification time of the remote file version
					touch('.'.SEPARATOR.$file, filemtime($remote_dir.$file));
					
					$this->data[updatedFiles][] = $file;
				}
			}

			//runs through all update paths
//			foreach ($updatePaths as $title => $path)
//			{
//				File::ScanDirectories($remote_dir.$path, array('php'), array('.', '..', 'svn', '.svn'), &$files, true);
//				
//				//runs through all files in the update path
//				$this->CompareFiles(&$files);
//			}			
//			
//			//$this->data[updates] = $result;
//			Debugger::VarDump($files);
//		}
//
//		public function CompareFiles(&$files)
//		{
//			//Debugger::VarDump($files);
//			
//			$i = 0;
//			foreach ($files as &$file)
//			{
//				if (!empty($file[sub]))
//				{
//					$this->CompareFiles(&$file[sub]);
//					continue;
//				}
//				
//				$fileInfo = pathinfo($file);
//				if ((empty($fileInfo[filename])) || ($fileInfo[filename] == '.')) continue;
//				
//				//$result[$title][$i][path] = $path.SEPARATOR.$file;
//				
//				//if this is a new file
//				if (!file_exists('.'.SEPARATOR.$path.SEPARATOR.$file))
//				{
//					$result[$title][$i][status] = '-';
//					$i++;
//					continue;
//				}
//				
//				$remoteFileTime = filemtime($remote_dir.$path.SEPARATOR.$file);
//				$currentFileTime = filemtime('.'.SEPARATOR.$path.SEPARATOR.$file);
//				
//				//if the remote file is newer
//				if ($remoteFileTime > $currentFileTime)
//				{
//					$result[$title][$i][status] = '>';
//				}
//				//if the remote file is older
//				elseif ($remoteFileTime < $currentFileTime)
//				{
//					$result[$title][$i][status] = '<';
//				}
//				//if the remote file is the same
//				elseif ($remoteFileTime = $currentFileTime)
//				{
//					$result[$title][$i][status] = '=';
//				}
//				
//				$result[$title][$i][remoteTime] = $remoteFileTime;
//				$result[$title][$i][localTime] = $currentFileTime;
//				
//				$i++;
//			}
//			//return $result;
//		}
			
			//old version
			foreach ($updatePaths as $title => $path)
			{
				$files = scandir($remote_dir.$path);
				
				//runs through all files in the update path
				$i = 0;
				foreach ($files as $file)
				{
					$fileInfo = pathinfo($file);
					if ((empty($fileInfo[filename])) || ($fileInfo[filename] == '.')) continue;
					
					$result[$title][$i][path] = $path.SEPARATOR.$file;
					
					//if this is a new file
					if (!file_exists('.'.SEPARATOR.$path.SEPARATOR.$file))
					{
						$result[$title][$i][status] = '-';
						$i++;
						continue;
					}
					
					$remoteFileTime = filemtime($remote_dir.$path.SEPARATOR.$file);
					$currentFileTime = filemtime('.'.SEPARATOR.$path.SEPARATOR.$file);
					
					//if the remote file is newer
					if ($remoteFileTime > $currentFileTime)
					{
						$result[$title][$i][status] = '>';
					}
					//if the remote file is older
					elseif ($remoteFileTime < $currentFileTime)
					{
						$result[$title][$i][status] = '<';
					}
					//if the remote file is the same
					elseif ($remoteFileTime = $currentFileTime)
					{
						$result[$title][$i][status] = '=';
					}
					
					$result[$title][$i][remoteTime] = $remoteFileTime;
					$result[$title][$i][localTime] = $currentFileTime;
					
					$i++;
				}
			}
			
			$this->data[updates] = $result;
		}
		
		/**
		 * Removes backup files (with .bak extension)
		 *
		 */
		public function RemoveBackups()
		{
			$files = scandir('.'.$this->request->get[dir]);
			foreach ($files as $file)
			{
				$path = pathinfo($file);
				if ($path[extension] == 'bak')
				{
					unlink('.'.$this->request->get[dir].SEPARATOR.$file);
				}
			}
		}
		
	}
?>